//
//  ProjectView.m
//  nova
//
//  Created by user on 11/05/29.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ProjectView.h"


@implementation ProjectView
@synthesize project;
// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization.
    }
    return self;
}
*/


- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated {
	[super viewWillAppear:animated];
	[self initObjects];
}


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[self initObjects];
}



-(void) initObjects{
	project = [myAppDelegate getProject];
	txtName.text = [project getProjectName];
	
	/*NSLocale *currentUsersLocale = [NSLocale currentLocale];
	 NSDateFormatter *df = [[NSDateFormatter alloc] init];
	 NSString *dateFormat = [NSDateFormatter dateFormatFromTemplate:@"EdMMMYYYY" options:0 locale:currentUsersLocale];
	 [df setDateFormat:dateFormat];	
	 
	 
	 NSDate *projectStartDate  = [NSDate dateWithTimeIntervalSince1970:[project getProjectStartTime]];
	 NSDate *projectFinishDate  = [NSDate dateWithTimeIntervalSince1970:[project getProjectFinishTime]];
	 NSDate *projectStatusDate  = [NSDate dateWithTimeIntervalSince1970:[project getProjectStatusTime]];
	 */
	[btStartDate setTitle: [Library getLocalizedDateString: [project getProjectStartTime]] forState:UIControlStateNormal ];
	[btFinishDate setTitle: [Library getLocalizedDateString:[project getProjectFinishTime]] forState:UIControlStateNormal];
	[btStatusDate setTitle: [Library getLocalizedDateString:[project getProjectStatusTime]] forState:UIControlStateNormal ];
	txtDescription.text = [project getProjectDescription];
	
	
	
	NSDate *now = [[NSDate alloc] init];
	double nowTimeInterval = [now timeIntervalSince1970];	
	[now dealloc];
	
	double totalProgress =  ([project getProjectStartTime] + nowTimeInterval * 100) / [project getProjectFinishTime];
	
	if(totalProgress > 100)
	{
		progressLabel.text = @"100";
	}else{
		progressLabel.text = [NSString stringWithFormat:@"%.2f",totalProgress];
    }
	[progress setProgress:(([project getProjectStartTime] + nowTimeInterval) / [project getProjectFinishTime])];
	
	[scroll
	 setContentSize:CGSizeMake(320,
							   630)];
	backgroundView.backgroundColor = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"background.jpg"]];
	
	[btStatus setTitle:[project getProjectStatus] forState:UIControlStateNormal];
	
	[super viewDidLoad];
	
}




/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations.
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];    
    // Release any cached data, images, etc. that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
}

-(IBAction) btStartDateClick : (id) sender{
	DatePicker *datePicker = [[DatePicker alloc] initWithNibName:@"DatePicker" bundle:[NSBundle mainBundle]];
	[datePicker setTargetTimeSelector: [project getSetProjectStartTimeSelector] ];
	[datePicker setTargetObject:project];
	[datePicker setTargetButton:(UIButton *)sender];
	[self.view addSubview:datePicker.view];
}
-(IBAction) btFinishDateClick : (id) sender{
	DatePicker *datePicker = [[DatePicker alloc] initWithNibName:@"DatePicker" bundle:[NSBundle mainBundle]];
	[datePicker setTargetTimeSelector : [project getSetProjectFinishTimeSelector] ];
	[datePicker setTargetObject:project];
	[datePicker setTargetButton:(UIButton *)sender];
	[self.view addSubview:datePicker.view];
}
-(IBAction) btStatusDateClick : (id) sender{
	DatePicker *datePicker = [[DatePicker alloc] initWithNibName:@"DatePicker" bundle:[NSBundle mainBundle]];
	[datePicker setTargetTimeSelector : [project getSetProjectStatusTimeSelector] ];
	[datePicker setTargetObject:project];
	[datePicker setTargetButton:(UIButton *)sender];
	[self.view addSubview:datePicker.view];
}
-(IBAction) btStatusClick : (id) sender{
	PickerView *statusPicker = [[PickerView alloc] initWithNibName:@"PickerView" bundle:[NSBundle mainBundle]];
	[self.view addSubview:statusPicker.view];
}
-(IBAction) btCancelClick : (id) sender{
	[myAppDelegate cancelProjectEditing];
}

-(IBAction) txtNameChanged : (id) sender{
	[project setProjectName:[txtName text] ];
}

-(IBAction) txtDescriptionChanged : (id) sender{
	[project setProjectDescription:[txtDescription text] ];
}

-(IBAction) backgroundClicked : (id) sender{
	[txtName resignFirstResponder];
	[txtDescription resignFirstResponder];
}

-(IBAction) btSave : (id) sender{
	[project setProjectName:[txtName text] ];
	[project setProjectDescription:[txtDescription text] ];
	DataContext * context = [[DataContext alloc] init];
	if(project.isNew){
		[context executeQuery:[project getInsertString]];
		project.isNew = false;
		project.changed = false;
	}else {
		if(project.changed){
			[context executeQuery:[project getUpdateString]];
			project.changed = false;
		}
	}
	[myAppDelegate cancelProjectEditing];
}

- (void)dealloc {
    [super dealloc];
}


@end
